package cn.tedu.ivos.base.file;
import cn.tedu.ivos.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/v1/file/")
public class UploadController {
    @PostMapping("upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf('.'));
        filename = UUID.randomUUID()+suffix;
        System.out.println(filename);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
//        日期路径
        String dataPath = simpleDateFormat.format(new Date());
        String dirPath = "D://files";
        File dirFile = new File(dirPath+dataPath);
        if (!dirFile.exists())
        {
            dirFile.mkdirs();
        }
        String filePath = dirPath + dataPath + filename;

        file.transferTo(new File(filePath));
        return JsonResult.ok(dataPath+filename);
    }
    @PostMapping("remove")
    public JsonResult remove(String imgUrl)
    {
        log.debug("删除照片{}",imgUrl);
        new File("D:/files"+imgUrl).delete();
        return JsonResult.ok();
    }
}
